// run: valgrind --leak-check=full --main-stacksize=83886080 $exec < outpu
//#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <vector>
#include <list>

struct edge { int index; int to; int from; };

int const maxn = 200007;
int const maxm = 300007;
bool vis_edge[maxm];
int indegree[maxn];
edge edge_in[maxm];
int edge_color[maxm];
int n, m;

typedef std::list<edge> edge_type;
typedef edge_type::iterator edge_it;
typedef std::vector<edge_type> graph_type;
edge_type graph[maxn];

bool eulerian_tour_odd_path(int x) // 1: white  0: black
{
	for (edge_it it = graph[x].begin(); it != graph[x].end(); ) {
		int v = (*it).to, index = (*it).index;

		if (vis_edge[index]) {
			it = graph[x].erase(it);
			continue;
		}
		vis_edge[index] = true;
		indegree[x]--; indegree[v]--;

		graph[x].erase(it);
		if (eulerian_tour_odd_path(v)) {
			if (x == edge_in[index].from) edge_color[index] = 0;
			else						  edge_color[index] = 1;
			return true;
		}
		it = graph[x].begin();
	}
	return true;
}

void eulerian_tour(int x, int id)
{
	for (edge_it it = graph[x].begin(); it != graph[x].end(); ) {
		int v = (*it).to, index = (*it).index;
		if (vis_edge[index]) {
			it = graph[x].erase(it);
			continue;
		}
		vis_edge[index] = true;
		indegree[x]--; indegree[v]--;
		graph[x].erase(it);
		eulerian_tour(v, index);
		it = graph[x].begin();
	}
	if (id != -1) {
		if (x == edge_in[id].to) edge_color[id] = 0;
		else 				     edge_color[id] = 1;
	}
}

int main()
{
	int T;
	std::scanf("%d", &T);
	while (T--) {
		std::scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++) graph[i].clear();
		for (int i = 0; i < m; i++) vis_edge[i] = false;
		for (int i = 0; i < n; i++) indegree[i] = 0;

		for (int i = 0, x, y; i < m; i++) {
			std::scanf("%d%d", &x, &y);
			x--; y--;
			edge tmp; tmp.index = i;
			tmp.to = y; graph[x].push_back(tmp);
			tmp.to = x; graph[y].push_back(tmp);
			tmp.from = x; tmp.to = y;
			edge_in[i] = tmp;
			indegree[x]++; indegree[y]++;
		}

		for (int i = 0; i < n; i++)
			if (indegree[i] & 1) {
				eulerian_tour_odd_path(i);
			}

		for (int i = 0; i < n; i++)
			if (indegree[i]) {
				eulerian_tour(i, -1); // FIXME
			}

		for (int i = 0; i < m; i++)
			std::printf("%d\n", edge_color[i]);
	}
}

